package video.com.activeMQ.activeMQ;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.utils.GenerateNum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import video.com.activeMQ.MqConstants;
import video.com.entity.VideoApprove;
import video.com.service.VideoApproveService;
import video.com.service.VideoService;
import video.com.utils.IdCreator;

import java.util.Map;

@Slf4j
@Component
public class Listener {

    @Autowired
    private VideoApproveService approveService;

    @Autowired
    private VideoService videoService;


    /**
     * 将待审核的视频信息放入数据库
     * @param videoId mq中获取的待审核的videId
     * @throws Exception
     */
    @JmsListener(destination = MqConstants.PUBLISH_TOPIC)
    public void addApprove(String videoId) throws Exception {
        VideoApprove one = approveService.getOne(new LambdaQueryWrapper<VideoApprove>().eq(VideoApprove::getVideoId, videoId));

        if(one != null){
            return;
        }
        one = new VideoApprove();
        one.setVideoId(videoId);
        one.setApproveId(GenerateNum.generateOrder());

        //分配对应的视频审核员
        Map<String, Object> quickApproveUser = approveService.getQuickApproveUser();

        if(quickApproveUser.isEmpty()){
            return;
        }
        one.setApproveUser(quickApproveUser.get("approveUser").toString());
        try{
            approveService.save(one);
        }catch (Exception e){
            e.printStackTrace();
            log.info("数据库保存时发生异常");
        }

    }

}
